package indi.zn.commons.collection;

import java.util.Map;
import java.util.function.UnaryOperator;

public class Maps {

	private Maps() {
	}

	public static <K, V> Map<K, V> newMap() {
		return com.google.common.collect.Maps.newHashMap();
	}


	public static <K, V> Map<K, V> newMap(K key, V value) {

		Map<K, V> map = com.google.common.collect.Maps.newHashMap();

		map.put(key, value);

		return map;

	}

	public static <K, V> Map<K, V> newMap(Map<? extends K, ? extends V> emap) {

		Map<K, V> map = com.google.common.collect.Maps.newHashMap(emap);

		return map;

	}
	
	public static <K, V> Map<K, V> newMap(UnaryOperator<Map<K, V>>  builder) {

		Map<K, V> map = com.google.common.collect.Maps.newHashMap();
		
		builder.apply(map);
		
		return map;
	
	}
	
	
	public static <K, V> Map<K, V> newMap(UnaryOperator<Map<K, V>>  builder,MapType type) {

		Map<K, V> map =newMap(type);
		
		builder.apply(map);
		
		return map;
	
	}
	

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static <K, V> Map<K, V> newMap(MapType type) {

		Map map = null;

		if (MapType.HashMap.equals(type)) {
			map = com.google.common.collect.Maps.newHashMap();
		} else if (MapType.ConcurrentMap.equals(type)) {
			map = com.google.common.collect.Maps.newConcurrentMap();
		} else if (MapType.TreeMap.equals(type)) {
			map = com.google.common.collect.Maps.newTreeMap();
		} else if (MapType.IdentityHashMap.equals(type)) {
			map = com.google.common.collect.Maps.newIdentityHashMap();
		} else if (MapType.LinkedHashMap.equals(type)) {
			map = com.google.common.collect.Maps.newLinkedHashMap();
		}

		return map;

	}

	public static <K, V> Map<K, V> newMap(MapType type, K key, V value) {

		Map<K, V> map = newMap(type);

		map.put(key, value);

		return map;

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static <K, V> Map<K, V> newMap(MapType type,
			Map<? extends K, ? extends V> emap) {

		Map map = null;

		if (MapType.HashMap.equals(type)) {
			map = com.google.common.collect.Maps.newHashMap(emap);
		} else if (MapType.ConcurrentMap.equals(type)) {
			map = com.google.common.collect.Maps.newConcurrentMap();
		} else if (MapType.TreeMap.equals(type)) {
			map = com.google.common.collect.Maps.newTreeMap();
		} else if (MapType.IdentityHashMap.equals(type)) {
			map = com.google.common.collect.Maps.newIdentityHashMap();
		} else if (MapType.LinkedHashMap.equals(type)) {
			map = com.google.common.collect.Maps.newLinkedHashMap(emap);
		}

		return map;

	}
	
	
	public  static <K, V> MapBuilder<K, V>  getMapbuilder(){
		
		return new MapBuilder<K, V>();
	
	}
	
   public  static <K, V> MapBuilder<K, V>  getMapbuilder(MapType type){
		
		return new MapBuilder<K, V>(type);
	
	}

}
